package com.example.reader.controller;

import com.example.reader.common.Result;
import com.example.reader.entity.Comment;
import com.example.reader.service.CommentService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/comment")
public class CommentController {
    @Autowired
    private CommentService commentService;

    //查询评论模板
    @GetMapping("/selectTree/{module}/{fid}")
    public Result selectTree(@PathVariable String module, @PathVariable Integer fid,
                             @RequestParam Integer pageNum,
                             @RequestParam Integer pageSize) {
        PageInfo<Comment> pageInfo = commentService.selectTree(module, fid, pageNum, pageSize);
        return Result.success(pageInfo);
    }
    //查询评论的数量
    @GetMapping("/selectCount/{module}/{fid}")
    public Result selectCount(@PathVariable String module, @PathVariable Integer fid) {
        Integer count = commentService.selectCount(module, fid);
        return Result.success(count);
    }

    //递归删除
    @DeleteMapping("/deepDelete/{pid}")
    public Result deepDelete(@PathVariable Integer pid) {
        commentService.deepDelete(pid);
        return Result.success();
    }

    //评论
    @PostMapping("/add")
    public Result add(@RequestBody Comment comment) {
        commentService.add(comment);
        return Result.success();
    }
    //分页查询
    @GetMapping("/selectPage")
    public Result selectPage(Comment comment,
                             @RequestParam(defaultValue = "1") Integer pageNum,
                             @RequestParam(defaultValue = "10") Integer pageSize) {
        PageInfo<Comment> pageInfo = commentService.selectPage(comment, pageNum, pageSize);
        return Result.success(pageInfo);
    }
}
